// 阅读器一些设置
const readerSetting = uni.getStorageSync('readerSetting') || {};

const reader = {
  state: {
    bookid: '',
    name: '',
    chapterInfo: '',
    chapterNum: 1,
    maxChapterNum: 1,
    menuName: 'default',
    showMenu: false,
    theme: readerSetting.theme || 'default',
    fontSize: readerSetting.fontSize || 16, // 字体大小
    bright: readerSetting.bright || 1,
    isAutoBuy: readerSetting.isAutoBuy || false,
    noScroll: false, // 禁止页面滚动
    updateStatus: '',
    goodPrice: 0,
    userAmount: 0,
    modeName: 'scroll', // 阅读模式
  },

  mutations: {
    updateReader(state, payload = {}) {
      Object.keys(payload).forEach(key => {
        state[key] = payload[key];
      });
    },

    saveReaderSetting(state) {
      // 存储阅读器设置
      uni.setStorageSync('readerSetting', {
        fontSize: state.fontSize,
        bright: state.bright,
        isAutoBuy: state.isAutoBuy,
        theme: state.theme,
        modeName: state.modeName,
      });
    },

    resetReader(state) {
      state.bookid = '';
      state.name = '';
      state.chapterInfo = '';
      state.chapterNum = 1;
      state.maxChapterNum = 1;
      state.menuName = 'default';
      state.showMenu = false;
      state.modeName = 'scroll';

      const readerSetting = uni.getStorageSync('readerSetting') || {};
      state.theme = readerSetting.theme || 'default';
      state.fontSize = readerSetting.fontSize || 16; // 字体大小
      state.bright = readerSetting.bright || 1;
      state.isAutoBuy = readerSetting.isAutoBuy || false;
      state.modeName = readerSetting.modeName || 'scroll';
      state.noScroll = false; // 禁止页面滚动
      state.updateStatus = '';
    }
  }
};

export default reader;
